package com.zyf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zyf.model.hosp.Department;
import com.zyf.repository.DepartmentRepository;
import com.zyf.service.DepartmentService;
import com.zyf.vo.hosp.DepartmentQueryVo;
import com.zyf.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 医院科室的Service实现类
 *
 * @author zyf
 * @date 2021年05月07日 18:16
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传医院科室信息Api
     *
     * @param paramMap 医院科室信息的Map集合
     * @author zyf
     * @date 2021/5/7 18:27
     */
    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        //paramMap 转换为Department 对象
        String map = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(map, Department.class);

        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (departmentExist != null) {
            department.setUpdateTime(new Date())
                    .setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date())
                    .setUpdateTime(new Date())
                    .setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 分页查询医院科室API
     *
     * @param page    当前页码
     * @param limit   每页记录数
     * @param queryVo 条件查询参数的Vo对象
     * @return Page<com.zyf.model.hosp.Department>
     * @author zyf
     * @date 2021/5/8 9:54
     */
    @Override
    public Page<Department> findDepartmentByPage(int page, int limit, DepartmentQueryVo queryVo) {
        //创建Pageable对象，设置当前页和每页记录数
        //0 是第一页
        Pageable pageable = PageRequest.of(page - 1, limit);

        //创建Examole对象
        Department department = new Department();
        if (queryVo != null) {
            BeanUtils.copyProperties(queryVo, department);
        }
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);


        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 根据医院编号和科室编号删除医院科室API
     *
     * @param hoscode 医院编号
     * @param depcode 科室编号
     * @author zyf
     * @date 2021/5/8 10:59
     */
    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 查询医院所有科室列表
     * @author zyf
     * @date 2021/5/13 17:00
     * @param hoscode 医院编号
     * @return java.util.List<com.zyf.vo.hosp.DepartmentVo>
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> list = new ArrayList<>();

        //根据医院编号查询所有科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> deptBigList = departmentRepository.findAll(example);

        //用Stream分组 根据大科室编号 bigcode，获取每个大科室里面的下级科室
        Map<String, List<Department>> deptMap = deptBigList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        //遍历Map
        for (Map.Entry<String,List<Department>> entry : deptMap.entrySet()){
            //得到大科室编号
            String bigcode = entry.getKey();
            //得到大科室里面的小科室
            List<Department> deptList = entry.getValue();


            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigcode)
                    .setDepname(deptList.get(0).getDepname());

            //封装小科室
            List<DepartmentVo> deptVoList = new ArrayList<>();
            deptList.forEach(dept ->{
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(dept.getDepcode())
                        .setDepname(dept.getDepname());
                deptVoList.add(departmentVo1);
            });

            //把小科室的list集合放到大科室的children中
            departmentVo.setChildren(deptVoList);

            //放入list集合中
            list.add(departmentVo);
        }

        return list;
    }

    /**
     * 根据医院编号和科室编号得到科室名称
     * @author zyf
     * @date 2021/5/14 15:02
     * @param hoscode 医院编号
     * @param depcode 科室编号
     * @return java.lang.String
     */
    @Override
    public String getDeptName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
